const MyPromise = require("../template.js")

/** 十六、Promise.catch 方法测试 */

// 测试 1: 基本 catch
function testCatchBasic() {
  MyPromise.reject("Error").catch((reason) =>
    console.log("16.1 Catch basic:", reason === "Error")
  )
}

// 测试 2: catch 等价于 then(null, onRejected)
function testCatchEquivalent() {
  let caught1 = false
  let caught2 = false

  MyPromise.reject("Test").catch((reason) => {
    caught1 = reason === "Test"
  })

  MyPromise.reject("Test").then(null, (reason) => {
    caught2 = reason === "Test"
  })

  setTimeout(() => {
    console.log("16.2 Catch equivalent to then:", caught1 && caught2)
  }, 10)
}

// 测试 3: catch 捕获 then 中的异常
function testCatchInThen() {
  MyPromise.resolve(1)
    .then((value) => {
      throw new Error("Then Error")
    })
    .catch((reason) => console.log("16.3 Catch then error:", reason.message === "Then Error"))
}

// 测试 4: catch 后可以继续 then
function testCatchThenChain() {
  MyPromise.reject("Error")
    .catch((reason) => {
      console.log("16.4 Catch then chain - catch:", reason === "Error")
      return "Recovered"
    })
    .then((value) => console.log("16.5 Catch then chain - then:", value === "Recovered"))
}

// 测试 5: catch 不会捕获后续 then 中的错误
function testCatchNotForward() {
  MyPromise.resolve(1)
    .catch((reason) => {
      console.log("16.6 Catch not forward:", false) // 不应该执行
    })
    .then((value) => {
      console.log("16.6 Catch not forward:", value === 1)
      throw new Error("After catch")
    })
    .catch((reason) => console.log("16.7 Catch after error:", reason.message === "After catch"))
}

// 测试 6: catch 中抛出新错误
function testCatchThrowNew() {
  MyPromise.reject("First")
    .catch((reason) => {
      console.log("16.8 Catch throw new - first:", reason === "First")
      throw new Error("Second")
    })
    .catch((reason) => console.log("16.9 Catch throw new - second:", reason.message === "Second"))
}

// 测试 7: catch 返回 Promise
function testCatchReturnPromise() {
  MyPromise.reject("Error")
    .catch((reason) => {
      console.log("16.10 Catch return Promise - catch:", reason === "Error")
      return MyPromise.resolve("New Value")
    })
    .then((value) => console.log("16.11 Catch return Promise - then:", value === "New Value"))
}

// 测试 8: catch 返回 rejected Promise
function testCatchReturnRejected() {
  MyPromise.reject("First Error")
    .catch((reason) => {
      console.log("16.12 Catch return rejected - first:", reason === "First Error")
      return MyPromise.reject("Second Error")
    })
    .catch((reason) => console.log("16.13 Catch return rejected - second:", reason === "Second Error"))
}

// 测试 9: 多个 catch
function testMultipleCatch() {
  MyPromise.reject("Error")
    .catch((reason) => {
      console.log("16.14 Multiple catch - first:", reason === "Error")
      return "Handled"
    })
    .catch((reason) => {
      console.log("16.15 Multiple catch - second:", false) // 不应该执行
    })
    .then((value) => console.log("16.15 Multiple catch - second:", value === "Handled"))
}

// 测试 10: catch 处理 constructor 中的异常
function testCatchConstructorError() {
  new MyPromise(() => {
    throw new Error("Constructor Error")
  }).catch((reason) => console.log("16.16 Catch constructor error:", reason.message === "Constructor Error"))
}

testCatchBasic()
testCatchEquivalent()
testCatchInThen()
testCatchThenChain()
testCatchNotForward()
testCatchThrowNew()
testCatchReturnPromise()
testCatchReturnRejected()
testMultipleCatch()
testCatchConstructorError()

